﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Common.Localization;

namespace UI
{
    public enum CellType { Active, Available }

    public partial class Cell : UserControl, ILocalizable
    {
        #region private fields

        private Color currentColor;
        private float value;
        private CellType type;

        #endregion

        #region constructors

        public Cell(CellType cellType)
        {
            this.type = cellType;

            this.InitializeComponent();

            this.Color = Color.Purple;
            Cell.Height = base.Height = 85;
            Cell.Width = base.Width = 187;
            Cell.Margin = 1;


            switch (this.type)
            {
                case CellType.Available:
                    {
                        this.decoratorLink.Text = LocalizationProvider.GetMessage("activate");
                        break;
                    }
                case CellType.Active:
                    {
                        this.decoratorLink.Text = @"X";

                        this.decoratorLink.LinkColor = Color.Tomato;
                        break;
                    }
            }

            this.decoratorLink.LinkBehavior = LinkBehavior.HoverUnderline;
            this.decoratorLink.Left = base.Width - this.decoratorLink.Width - 1;
            this.Controls.Add(this.decoratorLink);
        }

        static Cell()
        {
            Cell.Height = 85;
            Cell.Width = 187;
            Cell.Margin = 1;
        }

        #endregion

        #region events

        public event EventHandler CellDecoratorClicked;

        public event EventHandler CellOptionsClicked;

        #endregion

        #region public properties

        public Color Color
        {
            get
            {
                return this.currentColor;
            }

            set
            {
                this.currentColor = value;
                this.DragPanel.BackColor = value;
                this.Invalidate();
            }
        }

        public float Value
        {
            get
            {
                return this.value;
            }

            set
            {
                this.value = value;
                this.valueLabel.Text = string.Format("{0} %", Math.Round(value, 2));
                this.valueLabel.Left = base.Width - this.valueLabel.Width - 5;
            }
        }

        public string HeaderText
        {
            get
            {
                return this.headerLabel.Text;
            }

            set
            {
                this.headerLabel.Text = value;
            }
        }

        public static int Height { get; private set; }

        public static int Width { get; private set; }

        public static int Margin { get; private set; }

        public Panel DragPanel
        {
            get
            {
                return this.dragPnl;
            }
            private set
            {
                this.dragPnl = value;
            }
        }

        #endregion

        private void CellPaint(object sender, PaintEventArgs e)
        {
            Rectangle border = new Rectangle(0, 0, base.Width - 1, base.Height - 1);
            e.Graphics.DrawRectangle(new Pen(this.Color), border);
            //Rectangle rect = new Rectangle(1, 0, 20, this.Height - 1);
            //e.Graphics.FillRectangle(new SolidBrush(this.Color), rect);
        }

        private void DecoratorClicked(object sender, EventArgs e)
        {
            if (this.CellDecoratorClicked != null)
            {
                this.CellDecoratorClicked(this, null);
            }
        }

        public void Localize()
        {
            this.optionsLinkLabel.Text = LocalizationProvider.GetMessage("options");

            switch (this.type)
            {
                case CellType.Available:
                    this.decoratorLink.Text = LocalizationProvider.GetMessage("activate");
                    break;
            }
        }

        private void OptionsLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.CellOptionsClicked != null)
            {
                this.CellOptionsClicked(this, null);
            }
        }
    }
}
